﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient.Credentials
Imports System.Runtime.Serialization
Imports System.IO

Namespace Serializers

    Public Class QueueDataContractSerializer
        Implements IDataContractSerializer(Of ICloudQueue)
        Private ReadOnly serializer As DataContractSerializer
        Private ReadOnly storageCredentials As IStorageCredentials

        Public Sub New(ByVal credentials As IStorageCredentials)
            Me.serializer = New DataContractSerializer(GetType(StorageAccountQueueListResponse))
            Me.storageCredentials = credentials
        End Sub

        Public Function ReadObject(ByVal stream As Stream) As IEnumerable(Of ICloudQueue) Implements IDataContractSerializer(Of ICloudQueue).ReadObject
            Dim results = TryCast(Me.serializer.ReadObject(stream), StorageAccountQueueListResponse)
            For Each queue In results.Queues
                queue.StorageCredentials = Me.storageCredentials
            Next queue

            Return results.Queues.Cast(Of ICloudQueue)()
        End Function
    End Class
End Namespace